package com.wyzz.hr.bo.impl;

import com.wyzz.hr.bo.IEmpSalaryAcctBo;
import com.wyzz.hr.dao.IEmpSalaryAcctDao;
import com.wyzz.hr.dao.IEmpSalaryAcctversionDao;
import com.wyzz.hr.pojo.Empsalaryacct;
import com.wyzz.hr.pojo.Empsalaryacctversion;
import com.wyzz.hr.util.Pager;

import java.util.List;
import org.hibernate.FetchMode;
import org.hibernate.criterion.*;

public class EmpSalaryAcctBoImpl implements IEmpSalaryAcctBo {

	public EmpSalaryAcctBoImpl() {
	}

	public List getObjects(Class clas, String fetch[]) {
		return empsalaryacctDao.getObjects(clas, fetch);
	}

	public Empsalaryacct loadObject(String id, String fetch[]) {
		return (Empsalaryacct) empsalaryacctDao.loadObject(Empsalaryacct.class,
				id, fetch, new boolean[0]);
	}

	public Boolean deleteEmpsalaryacct(Object id) {
		try {
			empsalaryacctDao.deleteObject(id);
			return Boolean.valueOf(true);
		} catch (Exception e) {
			return Boolean.valueOf(false);
		}
	}

	public Boolean deleteEmpsalaryacct(String esaId) {
		try {
			String hql = (new StringBuilder()).append(
					"delete Empsalaryacct as esa where id='").append(esaId)
					.append("'").toString();
			empsalaryacctDao.exeHql(hql);
			return Boolean.valueOf(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Boolean.valueOf(false);
	}

	public Boolean insertEmpsalaryacct(Empsalaryacct empsalaryacct) {
		try {
			empsalaryacctDao.saveObject(empsalaryacct);
			return Boolean.valueOf(true);
		} catch (Exception e) {
			return Boolean.valueOf(false);
		}
	}

	public boolean searchAcctNames(String acctName, String acctId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Empsalaryacctversion.class);
		detachedCriteria.setFetchMode("esavEsac", FetchMode.JOIN);
		detachedCriteria.createAlias("esavEsac", "esac");
		if (acctName != null)
			detachedCriteria.add(Restrictions.eq("esac.esacName", acctName
					.trim()));
		else
			return false;
		if (acctId != null)
			detachedCriteria.add(Restrictions.not(Restrictions.eq("esac.id",
					acctId.trim())));
		List list = empsalaryacctversionDao.findByCriteria(detachedCriteria);
		return list != null && list.size() != 0;
	}

	public List searchEmpsalaryacct(Empsalaryacct empsalaryacct, Pager page,
			String versionFlag) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Empsalaryacctversion.class);
		detachedCriteria.setFetchMode("esavEsac", FetchMode.JOIN);
		detachedCriteria.createAlias("esavEsac", "esac");
		if (empsalaryacct.getEsacName() != null)
			detachedCriteria.add(Restrictions.like("esac.esacName",
					empsalaryacct.getEsacName().trim(), MatchMode.ANYWHERE));
		if (versionFlag == null)
			detachedCriteria.add(Restrictions.isNull("esavValidTo"));
		checkOrderMethod(detachedCriteria, page.getOrder());
		detachedCriteria.addOrder(Order
				.desc(Empsalaryacctversion.PROP_ESAV_VALID_FROM));
		return empsalaryacctversionDao.findByCriteria(detachedCriteria);
	}

	public Empsalaryacctversion getMaxValidToEsav(String acctId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Empsalaryacctversion.class);
		detachedCriteria.setFetchMode("esavEsac", FetchMode.JOIN);
		detachedCriteria.createAlias("esavEsac", "esac");
		detachedCriteria.add(Restrictions.eq("esac.id", acctId));
		detachedCriteria.addOrder(Order.desc("esavValidTo"));
		List list = empsalaryacctversionDao.findByCriteria(detachedCriteria);
		return (Empsalaryacctversion) list.get(0);
	}

	public Boolean updateEmpsalaryacct(Empsalaryacct empsalaryacct) {
		try {
			empsalaryacctDao.updateObject(empsalaryacct);
			return Boolean.valueOf(true);
		} catch (Exception e) {
			return Boolean.valueOf(false);
		}
	}

	private void checkOrderMethod(DetachedCriteria detachedCriteria,
			String order) {
		if (order != null && order.trim().length() > 1
				&& order.indexOf('-') != -1) {
			String pram[] = order.split("-");
			if (pram[0] == null || pram[0].length() < 1)
				return;
			String fetch[] = pram[0].split("\\.");
			String orde = pram[0];
			if (fetch.length > 1) {
				String str = "";
				String fetc = "";
				for (int len = 0; len < fetch.length - 1; len++)
					if ("esavEsac".equals((new StringBuilder()).append(fetc)
							.append(fetch[len]).toString())) {
						fetc = (new StringBuilder()).append(fetc).append(
								fetch[len]).append(".").toString();
						str = "esac.";
					} else {
						detachedCriteria.createAlias((new StringBuilder())
								.append(fetc).append(fetch[len]).toString(),
								(new StringBuilder()).append("ord").append(len)
										.toString());
						fetc = (new StringBuilder()).append(fetc).append(
								fetch[len]).append(".").toString();
						str = (new StringBuilder()).append("ord").append(len)
								.append(".").toString();
					}

				orde = (new StringBuilder()).append(str).append(
						pram[0].substring(pram[0].lastIndexOf(".") + 1))
						.toString();
			}
			if (pram.length == 2 && pram[1].equalsIgnoreCase("up"))
				detachedCriteria.addOrder(Order.asc(orde));
			else
				detachedCriteria.addOrder(Order.desc(orde));
		}
	}

	public Empsalaryacctversion getAcctVersionByAcctId(String acctId) {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Empsalaryacctversion.class);
		detachedCriteria.add(Restrictions.isNull("esavValidTo"));
		detachedCriteria.add(Restrictions.eq("esavEsac.id", acctId));
		List versions = empsalaryacctDao.findByCriteria(detachedCriteria);
		if (versions.isEmpty())
			return null;
		else
			return (Empsalaryacctversion) versions.get(0);
	}

	public IEmpSalaryAcctDao getEmpsalaryacctDao() {
		return empsalaryacctDao;
	}

	public void setEmpsalaryacctDao(IEmpSalaryAcctDao empsalaryacctDao) {
		this.empsalaryacctDao = empsalaryacctDao;
	}

	public IEmpSalaryAcctversionDao getEmpsalaryacctversionDao() {
		return empsalaryacctversionDao;
	}

	public void setEmpsalaryacctversionDao(
			IEmpSalaryAcctversionDao empsalaryacctversionDao) {
		this.empsalaryacctversionDao = empsalaryacctversionDao;
	}

	public List searchAllEmpsalaryacct() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Empsalaryacct.class);
		return empsalaryacctDao.findByCriteria(detachedCriteria);
	}

	public List searchAllEmpsalaryacctVersionInUse() {
		DetachedCriteria detachedCriteria = DetachedCriteria
				.forClass(Empsalaryacctversion.class);
		detachedCriteria.add(Restrictions.isNull("esavValidTo"));
		detachedCriteria.setFetchMode("esavEsac", FetchMode.JOIN);
		return empsalaryacctDao.findByCriteria(detachedCriteria);
	}

	private IEmpSalaryAcctDao empsalaryacctDao;
	private IEmpSalaryAcctversionDao empsalaryacctversionDao;
}
